const wx2my = require('../wx2my');

class Fm {
  constructor() {
    this.host = 'http://ygd.yunchuang.store/';

    this.trim = str => {
      return str.replace(/(^\s*)|(\s*$)/g, "");
    };

    this.formatDate = (now, ff) => {
      let year = now.getFullYear();
      let month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;
      let date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
      let hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
      let minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
      let second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();

      if (ff == 'Y-m-d') {
        return year + "-" + month + "-" + date;
      } else if (ff == 'Y.m.d') {
        return year + "." + month + "." + date;
      } else if (ff == 'Y-m-d H:i:s') {
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
      } else if (ff == 'Y-m-d H:i') {
        return year + "-" + month + "-" + date + " " + hour + ":" + minute;
      }
    };
  }

  isNull = val => {
    return this.trim(val).length == 0 ? true : false;
  };
  formatTime = second => {
    /*秒转换成时间*/
    return [parseInt(second / 60 / 60) + '时', parseInt(second / 60 / 60) + '分', second % 60 + '分'].join(",").replace(/,/g, '');
  };
  format = (now, ff) => {
    /*日期格式化*/
    let time = time.toString();
    if (time.length == 10) time = time * 1000;
    let d = new Date(time * 1);
    return this.formatDate(d, ff);
  };
  checkIdCode = oldValue => {
    /*判断身份证格式是否正确*/
    let value = this.trim(oldValue);
    let userCardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    let taiwanreg = /^[A-Z][0-9]{9}$/; //台湾

    let xianggangreg = /^[A-Z][0-9]{6}\([0-9A]\)$/; //香港

    let aomenreg = /^[157][0-9]{6}\([0-9]\)$/; //澳门

    return userCardreg.test(value) || taiwanreg.test(value) || xianggangreg.test(value) || aomenreg.test(value);
  };
  checkPhone = value => {
    //手机验证
    return /^1\d{10}$/.test(value);
  };
  get_prevPage = oldPage => {
    //获取历史路由对象
    let page = oldPage ? parseInt(oldPage) + 1 : 2;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - page];
    if (prevPage == undefined || prevPage == null) return 0;
    return prevPage;
  };
  save = (key, val) => {
    wx2my.setStorageSync(key, val);
  };
  gain = key => {
    return wx2my.getStorageSync(key);
  };
  unsave = key => {
    wx2my.removeStorageSync(key);
  };
  getUrlParam = (name, url) => {
    let param = url.substr(url.indexOf('?') + 1);
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = param.match(reg);
    if (r != null) return unescape(r[2]);
    return '';
  };
  goTop = () => {
    if (wx2my.pageScrollTo) {
      wx2my.pageScrollTo({
        scrollTop: 0,
        duration: 0
      });
    } else {
      wx2my.showModal({
        title: '提示',
        content: '当前微信版本过低，可能会影响您的使用。'
      });
    }
  };
  wx_login = (cb, that) => {

    let this_ = this;

    my.getAuthCode ({
      scopes: ['auth_base'],
      success: function (e) {
        console.log("111getAuthCode11",e);
        let code = e.authCode;
        wx2my.setStorageSync('code', code);


    if (!wx2my.getStorageSync('token')) {
      my.getOpenUserInfo({
        lang: 'zh_CN',
        desc: '用于完善会员资料',

        success(res) {
          let userInfo = JSON.parse(res.response).response;
          console.log(userInfo);
          wx2my.setStorageSync('userInfo', userInfo);
          wx2my.setStorageSync('avatarUrl', userInfo.avatar);
          wx2my.setStorageSync('nickName', userInfo.nickName);
          // let code = wx2my.getStorageSync('code').data;
          let data = {
            code: code,
            avatarUrl: userInfo.avatar,
            gender: userInfo.gender == 1 ? '男' : '女',
            nickname: userInfo.nickName,
            province: userInfo.province,
            city: userInfo.city,
            type: "ali"
          };
          console.log("-=-==--=",data);

          data.type = 'ali';
          that.wxRequest('applet_login', data, function (res) {
            console.log(res);
  
            if (res.data.code == 1) {
              let token = res.data.data.token;
              wx2my.setStorageSync('token', token);
              typeof cb == "function" && cb(2);
            } else {
              wx2my.removeStorageSync('token');
              wx2my.showToast({
                icon: 'none',
                title: res.data.msg
              });
            }
          });
        
        },

        fail(res) {
          console.log('获取用户数据失败', res);
        }

      });
    }


  }
});



     };
}

;
export { Fm };